package com.scalar.chiptrack.operations.dataentry.inventoryTransit.actioncommands;

import com.scalar.chiptrack.actioncommands.ActionCommand;
import com.scalar.chiptrack.operations.dataentry.inventoryTransit.forms.InventoryTransitForm;
import com.scalar.chiptrack.operations.dataentry.dao.DataEntryDAO;
import com.scalar.chiptrack.operations.dataentry.LotDetails;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.process.dao.NewProductDAO;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;
import com.scalar.chiptrack.operations.release.dao.RelaseDAO;
import com.scalar.chiptrack.users.User;
import com.scalar.chiptrack.users.Functionality;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.sales.SubTotal;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: munugoti
 * Date: 15 Sep, 2010
 * Time: 6:53:46 PM
 * To change this template use File | Settings | File Templates.
 */
public class PreGrindDisposeSearchCommand extends ActionCommand
{
    private static final String ACTION_NAME = "/inventoryOutTrans.do" ;

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response) throws Exception
    {

        InventoryTransitForm invOutForm = (InventoryTransitForm) form;

        HashMap<String,String> errors = new HashMap<String,String>();
        HashMap<String,String> info   = new HashMap<String,String>();
        String action = request.getParameter("action");
        System.out.println("searchLots = " + action);
        ArrayList<LotDetails> lotList     = new ArrayList<LotDetails>();
        request.setAttribute(INFO_MESSAGES, info);
        request.setAttribute(ERROR_MESSAGES, errors);

        HttpSession mSession = request.getSession();
        User user = (User) mSession.getAttribute(SessionKeys.USER_KEY);

        if (user == null)
        {
            mSession.setAttribute(SessionKeys.SOURCE_PAGE_KEY, ACTION_NAME +"?" + request.getQueryString() );
            return (mapping.findForward("login"));
        }
        else
        {
            if (user.isActionAllowed(Functionality.DATA_ENTRY, User.USER_ACTION_MODIFY))
            {
                request.setAttribute("showMessage", "FALSE");
            }
            else
            {
                request.setAttribute("showMessage", "TRUE");
            }
        }

        invOutForm.setProcessStepId( 0 );
        invOutForm.setLocationId( 0 );
       try
       {
           if ("preGrindsearch".equalsIgnoreCase(action) )
           {
               invOutForm.setWipLotsList( null );
               lotList = DataEntryDAO.getAllRunningHoldLots( "PRE_GRIND" , invOutForm.getProcessStepId() , invOutForm.getLocationId(), invOutForm.getLotId()  );
               if ( lotList == null || lotList.size() == 0 )
               {
                    info.put("No PRE GRIND Inventory lots available" , null );
               }
               invOutForm.setWipLotsList( lotList);

               request.setAttribute("lotsList",   lotList );
               request.setAttribute("partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( "PRE_GRIND" ) );
               request.setAttribute("locationList",    VendorDAO.getInventoryLocationsByProcessByUserType( null, LOCATION_TYPE_WAFER_STORE , 0 ) );
               request.setAttribute("nextLocations",   RelaseDAO.loadProcessVendors( LOCATION_TYPE_WAFER_STORE, AdaptorConstants.VENDOR_NAME_SITIME) );
           }
           else if ("preGrindsave".equalsIgnoreCase(action) )
           {
                try
                {   DataEntryDAO.savePreGrindDisposeInfo( null, invOutForm, user.getUserName() );

                    resetToken( request );
                    info.put ( "Pre Grind Inventory Dispose Information Saved", null );
                }
                catch( Exception e )
                {
                    e.printStackTrace();
                    errors.put( "Error storing the Pre Grind Inventory Dispose Lot information", null );
                }

               lotList = DataEntryDAO.getAllRunningHoldLots( "PRE_GRIND" , invOutForm.getProcessStepId() , invOutForm.getLocationId(), invOutForm.getLotId()  );

               request.setAttribute("lotsList",   lotList );
               request.setAttribute("partNumbersList", ProductDao.loadAllPartsForProcessTypeByPsType( "PRE_GRIND" ) );
               request.setAttribute("locationList",    VendorDAO.getInventoryLocationsByProcessByUserType( null, LOCATION_TYPE_WAFER_STORE , 0 ) );
               request.setAttribute("nextLocations",   RelaseDAO.loadProcessVendors( LOCATION_TYPE_WAFER_STORE, AdaptorConstants.VENDOR_NAME_SITIME) );

               invOutForm.setProcessStepId( 0 );
               invOutForm.setLocationId( 0 );
               invOutForm.setWipLotsList( null);
           }

           request.setAttribute("action", action);
       }
       catch( Exception e)
       {
          errors.put( "Unable to get PRE_GRIND WIP/Inventory Lots", null);
          e.printStackTrace();
       }
       return mapping.findForward( "preGrindDisposeTrans" );
    }
}
